package com.tjbaobao.videoplayol.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceView;

/**
 * Created by TJbaobao on 2018/1/13.
 */

public class BaseSurfaceView extends SurfaceView {

    protected Context context ;
    public int viewWidth = 0,viewHeight = 0;

    public BaseSurfaceView(Context context) {
        this(context,null);
    }

    public BaseSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BaseSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs,defStyleAttr);
    }

    /**
     * 初始化组件
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    private void initView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        this.context = context ;
        onInitView(context, attrs, defStyleAttr);
    }
    protected void onInitView(Context context, AttributeSet attrs, int defStyleAttr) {

    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        this.viewWidth = measureWidth(widthMeasureSpec);
        this.viewHeight = measureHeight(heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 计算组件宽度
     */
    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = 0;
        if (specMode == MeasureSpec.AT_MOST) {
            result = specSize;
        }
        else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }

        return result;
    }

    /**
     * 计算组件高度
     */
    private int measureHeight(int measureSpec) {

        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = 0;
        if (specMode == MeasureSpec.AT_MOST) {
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        return result;
    }
}
